import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
  path: '/login',
  name: 'login',
  component: () =>
            import('@/views/login')
},
{
  path: '/home',
  name: 'home',
  component: () =>
            import('@/views/home'),
  redirect: '/welcome',
  children: [{
    // 不能设置为/home/welcome
    path: '/welcome',
    name: 'welcome',
    component: () =>
                    import('@/views/welcome')
  },
  {
    path: '/article',
    name: 'article',
    component: () =>
                    import('@/views/article')
  }, {
    path: '/articleadd',
    name: 'articleadd',
    component: () =>
                    import('@/views/articleadd')
  }, {
    path: '/articleedit/:aid',
    name: 'articleedit',
    component: () =>
                    import('@/views/articleedit')
  },
  {
    path: '/account',
    name: 'account',
    component: () =>
                    import('@/views/account')
  },
  {
    path: '/material',
    name: 'material',
    component: () =>
                    import('@/views/material')
  }
  ]
}

]

const router = new VueRouter({
  routes
})
// 设置路由守卫
router.beforeEach((to, from, next) => {
  var userinfo = window.sessionStorage.getItem('userinfo')
  // 如果用户是非登录页并且要跳转路径不是登录页面
  if (!userinfo && to.path !== '/login') {
    // 就跳转登录页面
    return next('/login')
  }
  // 否则忽略即可
  next()
})

export default router
